﻿using System;
using System.Data;

namespace OF.Common
{
    /// <summary> 
    ///GridViewHelper的摘要说明-生成树形结构的DataTable 
    /// </summary> 
    public class GridViewHelper
    {
        private string gridline;    //连接线 
        private DataTable dt;       //传入的DataTable 
        private DataTable dtOut;    //输出的DataTable 
        private DataRow row;        //用于从DataView取数据并增加到dtOut 
        public GridViewHelper()
        {
            // 
            //TODO: 在此处添加构造函数逻辑 
            // 
        }
        /// <summary> 
        /// 生成树形结构的DataTable 
        /// </summary> 
        /// <param name="datatable">原始数据表</param> 
        /// <param name="parentField">上级节点关键字段</param> 
        /// <param name="parentValue">上级节点值</param> 
        /// <param name="keyField">本节点关键字段</param> 
        /// <param name="textField">显示的文本字段</param> 
        /// <param name="sortString">排序字符串</param> 
        /// <returns>处理后的DataTable</returns> 
        public DataTable GetGridViewTreeData(DataTable datatable, string parentField, string parentValue, string keyField, string textField, string sortString)
        {
            datatable.Columns.Add("level", Type.GetType("System.Int32"));
            dt = datatable;
            dtOut = datatable.Clone();
            resetTextField(parentField, parentValue, keyField, textField, sortString, 0);
            return dtOut;
        }
        /// <summary> 
        /// 递归生成新的节点名称(带连接线) 
        /// </summary> 
        /// <param name="parentValueField">上级节点关键字段</param> 
        /// <param name="parentValue">上级节点值</param> 
        /// <param name="keyField">本节点关键字段</param> 
        /// <param name="textField">显示出来的文本字段</param> 
        /// <param name="sortString">排序字符串</param> 
        /// <param name="level">树深度</param> 
        /// <returns></returns> 
        private void resetTextField(string parentValueField, string parentValue, string keyField, string textField, string sortString, int level)
        {
            DataView dv = new DataView(dt, parentValueField + "='" + parentValue + "'", sortString, DataViewRowState.CurrentRows);
            int a = dv.Count;
            if (dv.Count == 0)
            {
                return;
            }
            for (int i = 0; i < a; i++)
            {
                gridline = "";
                dv.RowFilter = parentValueField + "='" + parentValue + "'";
                dv.Sort = sortString;
                getTreeLine(parentValueField, dv[i][parentValueField].ToString(), keyField, dv[i][keyField].ToString(), sortString);
                dv.RowFilter = parentValueField + "='" + parentValue + "'";
                dv.Sort = sortString;
                row = dtOut.NewRow();
                for (int c = 0; c < dv[i].Row.ItemArray.Length; c++)
                {
                    row[c] = dv[i][c];
                }
                dtOut.Rows.Add(row);
                dtOut.Rows[dtOut.Rows.Count - 1][textField] = gridline + (i == a - 1 ? "┗" : "┣") + dv[i][textField].ToString();
                dtOut.Rows[dtOut.Rows.Count - 1]["level"] = level;
                resetTextField(parentValueField, dv[i][keyField].ToString(), keyField, textField, sortString, level + 1);
            }
            dv.Dispose();
        }
        /// <summary> 
        /// 回溯生成树的连接线 
        /// </summary> 
        /// <param name="parentValueField">上级节点关键字段</param> 
        /// <param name="parentValue">上级节点值</param> 
        /// <param name="keyField">本节点关键字段</param> 
        /// <param name="nodeKey">本节点值</param> 
        /// <param name="sortString">排序字符串</param> 
        /// <returns></returns> 
        private void getTreeLine(string parentValueField, string parentValue, string keyField, string nodeKey, string sortString)
        {
            //选择父层节点 
            DataView dv = new DataView(dt, keyField + "='" + parentValue + "'", sortString, DataViewRowState.CurrentRows);
            if (dv.Count > 0)
            {
                //选择父节点同级节点 
                dv.RowFilter = parentValueField + "='" + dv[0][parentValueField].ToString() + "'";
                dv.Sort = sortString;
                for (int j = 0; j < dv.Count; j++)
                {
                    if (dv[j][keyField].ToString() == parentValue)
                    {
                        if (j == dv.Count - 1)
                        {
                            gridline = "　" + gridline;
                        }
                        else
                        {
                            gridline = "┃" + gridline;
                        }
                    }
                }
                getTreeLine(parentValueField, dv[0][parentValueField].ToString(), keyField, dv[0][keyField].ToString(), sortString);
            }
            dv.Dispose();
        }
    }
}